package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import com.guldan.jaina.dto.householdareainfo.HouseholdAreaInfoDTO;
import com.guldan.jaina.dto.householdproperty.HouseholdPropertyDTO;
import com.guldan.mapper.db.entity.HouseholdAreaInfo;
import com.guldan.mapper.db.entity.HouseholdProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.household.HouseholdDTO;
import com.guldan.jaina.dto.household.HouseholdSearchDTO;
import com.guldan.jaina.utils.HouseholdDomainConvertUtils;
import com.guldan.mapper.db.entity.Household;
import com.guldan.mapper.db.entity.HouseholdExample;
import com.guldan.mapper.db.mapper.HouseholdMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class HouseholdManager extends BaseManager {

    @Autowired
    private HouseholdMapperExt householdMapperExt;

    @Autowired
    private HouseholdAreaInfoManager householdAreaInfoManager;

    @Autowired
    private HouseholdPropertyManager householdPropertyManager;

    //获取单个家庭信息
    public HouseholdDTO get(Integer householdId) {
        if (householdId != null && householdId > 0) {
            HouseholdDTO dto = HouseholdDomainConvertUtils.convert(householdMapperExt.selectByPrimaryKey(householdId));
            dto.setHouseholdAreaInfo(householdAreaInfoManager.getByHouseholdId(householdId));
            dto.setHouseholdProperty(householdPropertyManager.getByHouseholdId(householdId));
            return dto;
        } else {
            return null;
        }
    }

    //新增/更新单个家庭，传入：HouseholdDTO
    public HouseholdDTO save(HouseholdDTO dto) {

        Household entity = null;

        //通过户口簿编号查询户口，如果库里不存在，则进行添加；否则，则进行更新
        HouseholdSearchDTO search = new HouseholdSearchDTO();
        search.setHouseholdRegisterNumber(dto.getHouseholdRegisterNumber());
        List<Household> list = householdMapperExt.selectByExample(buildSearchExample(search));
        if (list != null && list.size() > 0) {
            //如果存在，则取出
            entity = list.get(0);
            dto.setHouseholdId(entity.getHouseholdId());
        }

        Boolean isNew = false;

        if (entity == null) {
            //户口信息不存在，进行添加操作
            entity = new Household();
            entity.setHouseholdTypeCode(dto.getHouseholdTypeCode());
            entity.setHouseholdNumber(dto.getHouseholdNumber());
            entity.setHouseholdRegisterNumber(dto.getHouseholdRegisterNumber());
            entity.setRemark(dto.getRemark());

            entity.setCreateTime(DateUtil.getCurrentTime());
            //entity.setCreator("xxx");
            householdMapperExt.insertSelective(entity);
            dto.setHouseholdId(entity.getHouseholdId());
            isNew = true;
        } else {
            //户口信息存在，进行记录更新
            entity.setHouseholdTypeCode(dto.getHouseholdTypeCode());
            entity.setHouseholdNumber(dto.getHouseholdNumber());
            entity.setRemark(dto.getRemark());

            entity.setModifyTime(DateUtil.getCurrentTime());
            householdMapperExt.updateByPrimaryKeySelective(entity);

        }

        //开始保存household_area_info
        HouseholdAreaInfoDTO householdAreaInfoDTO = dto.getHouseholdAreaInfo() != null ? dto.getHouseholdAreaInfo() : new HouseholdAreaInfoDTO();
        householdAreaInfoDTO.setHouseholdId(entity.getHouseholdId());
        if (isNew) {
            //如果household为新增，则附属的表都为新增
            householdAreaInfoDTO.setHouseholdAreaInfoId(null);
        }
        dto.setHouseholdAreaInfo(householdAreaInfoManager.save(householdAreaInfoDTO));

        //开始保存household_property
        HouseholdPropertyDTO householdPropertyDTO = dto.getHouseholdProperty() != null ? dto.getHouseholdProperty() : new HouseholdPropertyDTO();
        householdPropertyDTO.setHouseholdId(entity.getHouseholdId());
        if (isNew) {
            //如果household为新增，则附属的表都为新增
            householdPropertyDTO.setHouseholdPropertyId(null);
        }
        dto.setHouseholdProperty(householdPropertyManager.save(householdPropertyDTO));

        //dto = HouseholdDomainConvertUtils.convert(entity);
        return get(dto.getHouseholdId());
    }

    //删除单个家庭信息
    public Boolean delete(Integer householdId) {
        Household entity = householdMapperExt.selectByPrimaryKey(householdId);
        entity.setIsDelete(true);
        return householdMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }

    //分页查询家庭信息列表
    public Pagination<HouseholdDTO> list(HouseholdSearchDTO householdSearchDTO) {
        Pagination<HouseholdDTO> result = new Pagination<HouseholdDTO>(householdSearchDTO.getCurrentPage(), householdSearchDTO.getPerPage());
        HouseholdExample example = buildSearchExample(householdSearchDTO);
        int total = householdMapperExt.countByExample(example);
        result.setTotalCount(total);
        if (total > 0) {
            List<HouseholdDTO> data = new ArrayList<HouseholdDTO>();
            List<Household> list = householdMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (Household entity : list) {
                HouseholdDTO dto = HouseholdDomainConvertUtils.convert(entity);
                data.add(dto);
            }
            result.setList(data);
        }
        return result;
    }

    //build查询Example
    private HouseholdExample buildSearchExample(HouseholdSearchDTO search) {
        HouseholdExample example = new HouseholdExample();
        HouseholdExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(search.getHouseholdRegisterNumber())) {
            criteria.andHouseholdRegisterNumberEqualTo(search.getHouseholdRegisterNumber());
        }
        if (search.getHouseholdId() != null && search.getHouseholdId() > 0) {
            criteria.andHouseholdIdEqualTo(search.getHouseholdId());
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("household_id asc");
        }
        return example;
    }

}
